package com.ghsc.ghxc.jsb.ui.my.activity

import android.os.Bundle
import android.text.TextUtils
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.ext.asColor
import com.cn.appcore.ext.setSingleClickListener
import com.cn.appcore.ext.showToast
import com.cn.appcore.http.constant.HttpConstant
import com.cn.appcore.utils.GsonUtil
import com.cn.appcore.utils.Preference
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.config.LoginHelper
import com.ghsc.ghxc.jsb.config.UserManager
import com.ghsc.ghxc.jsb.databinding.ActivityUpNicknameBinding
import com.ghsc.ghxc.jsb.ui.my.bean.UserBeanData

/**
 * 项目：GuoHuaMall
 * 包名：com.ghsc.ghxc.ui.my.activity
 * 类名：UpNicknameInfoActivity
 * 创建时间：2023/6/28 10:40
 * 创建人 @author： xiaolin
 * 描述 ：Todo...
 */
class UpNicknameInfoActivity : BaseVMActivity<UpNicknameInfoViewModel, ActivityUpNicknameBinding>(){
    override fun attachLayoutRes(): Int {
        return R.layout.activity_up_nickname
    }

    override fun attachVMClass(): Class<UpNicknameInfoViewModel>? {
        return UpNicknameInfoViewModel::class.java
    }
    var bundle: Bundle?=null
    var nickName:String=""

    var userInfo: UserBeanData?=null
    var userId: String by Preference(HttpConstant.USER_ID, "")
    override fun initBar() {
        super.initBar()
        mImmersionBar!!.statusBarDarkFont(true).init()
    }
    override fun initView() {
        bundle=intent.extras
        nickName=bundle!!.getString("nickName","")

        binding.run {
            bar.topLeftView.setOnClickListener {
                hideKeyboard(etName)
                finish()
            }
            bar.topTitle.text="修改昵称"
            bar.topTitle.setTextColor(R.color.black.asColor())
            binding.bar.topLeftImage.setImageResource(R.drawable.retu_left)
            etName.setText(nickName)

        }
        mViewModel.onUserInfoMethod("")
    }

    override fun initData() {
        super.initData()
        binding.tvBtnNext.setSingleClickListener {
            if (TextUtils.isEmpty(binding.etName.text)){
                showToast("昵称不可为空")
                return@setSingleClickListener
            }

            userInfo?.let {
                val map: java.util.HashMap<String, Any> = java.util.HashMap()
                map["nickname"] = binding.etName.text.toString().trim()
                map["headimg"] = it.headimg
                mViewModel.updateUserInfoMethod(GsonUtil.mapToJsonStrObjEncryption(map)!!)
            }


        }
    }

    override fun initRequestObserver() {
        super.initRequestObserver()
        mViewModel.apply {
            errorMsg.observe(this@UpNicknameInfoActivity) {
                showToast( it)
            }
            againLoginMsg.observe(this@UpNicknameInfoActivity) {
                showToast(CustKey.IN_AGAIN_MSG)
                LoginHelper.startLoginPage()
            }

            updateUserInfoData.observe(this@UpNicknameInfoActivity) {
                showToast("修改成功")
                finish()
            }
            onUserInfoData.observe(this@UpNicknameInfoActivity) {
                binding.run {
                    userInfo =it.data.data
                    userInfo?.let {
                        etName.setText(it.nickname)
                    }

                }
                //缓存个人信息
                UserManager.saveUserInfo(it.data.data)


            }
        }
    }

}
